package com.markerhub.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysCourseTime;
import com.markerhub.entity.SysLocation;
import com.markerhub.entity.SysTime;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;


/**
 * <p>
 *  前端控制器
 * </p>
 *page
 * @author wang
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/sys/time")
public class SysTimeController extends BaseController {


    @GetMapping("/listAll")//获取上课地点列表
    public Result listAll(String username){

        List<SysTime> pageData = sysTimeService.list(new QueryWrapper<SysTime>()
                .like(StrUtil.isNotBlank(username), "username", username));
        pageData.forEach(u ->{
            u.setShow("周"+u.getWeekday()+"第"+u.getTimespan()+"节");
        });
        return Result.succ(pageData);
    }

    @GetMapping("/list")//修改每节课的上下课时间
    public Result list(){
        List<SysTime> pageData = sysTimeService.list(new QueryWrapper<SysTime>()
                .like("weekday", 1));
        return Result.succ(pageData);
    }

    @PostMapping("/update")//修改每节课的上下课时间
    public Result update(@RequestBody SysTime sysTime){
        List<SysTime> ids=sysTimeService.getByTimesSpan(sysTime);

       Integer m=0;
       do{
           ids.get(m).setBeginTime(sysTime.getBeginTime());
           ids.get(m).setFinishTime(sysTime.getFinishTime());
           sysTimeService.updateById(ids.get(m));
           m=m+1;
       }while(m<5);
        return Result.succ("");
    }

    @PostMapping("/save")//新增节次
    public Result save(@RequestBody SysTime sysTime){


        Integer m=0;
        do{
            sysTime.setWeekday(m+1);
            sysTimeService.save(sysTime);
            m=m+1;
        }while(m<5);
        return Result.succ("");
    }

    @PostMapping("/delete")//删除节次
    public Result delete(@RequestBody SysTime sysTime){
        Map<String,Object> map = new HashMap<>();
        map.put("timespan",sysTime.getTimespan());
        sysTimeService.removeByMap(map);
        return Result.succ("");
    }


}
